import { AIGC_PROXY_HOST } from '@/config';
import { generateAuthorization } from '@/utils/ase';

import { Toast } from 'react-vant';

export const startPractice= async () => {
  const { timestamp, authorization } = generateAuthorization();
  let aiCharacterId=localStorage?.getItem('ai_character_id')
  let training_id = localStorage?.getItem('training_id')
  const response = await fetch(`${AIGC_PROXY_HOST}/ai-training/start-practice`, {
    method: 'POST',
    headers: {
      'Content-type': 'application/json',
      'Authorization': authorization,
      'Timestamp': String(timestamp),
    },
    body: JSON.stringify({
      training_id:Number(training_id),
      ai_character_id: aiCharacterId?Number(aiCharacterId):null,
    }),
  })
  const data = await response.json()
  // console.log(data,'startPractice')
  if(data.code===2000){
      return data 
  }else{
      Toast.info(data.msg)
  }
}

export const getPracticeConfig= async () => {
  let aiCharacterId=localStorage?.getItem('ai_character_id')
  let training_id = localStorage?.getItem('training_id')
  const { timestamp, authorization } = generateAuthorization();
  const response = await fetch(`${AIGC_PROXY_HOST}/ai-training/config?training_id=${training_id}&ai_character_id=${aiCharacterId}`, {
    method: 'GET',
    headers: {
      'Content-type': 'application/json',
      'Authorization': authorization,
      'Timestamp': String(timestamp),
    }
  })
  const data = await response.json()
  console.log(data,'getPracticeConfig')
  return data
}

export const finishPractice= async (practiceId: any, appId: any, roomId: any, taskId: any,duration:any,chatMessage:any) => {
  const { timestamp, authorization } = generateAuthorization();
  const response = await fetch(`${AIGC_PROXY_HOST}/ai-training/finish-practice`, {
    method: 'POST',
    headers: {
      'Content-type': 'application/json',
      'Authorization': authorization,
      'Timestamp': String(timestamp),
    },
    //PracticeId int64 `json:"practice_id"`
    //     Duration int64 `json:"duration"`
    //     ChatMessage []ChatMessage `json:"chat_message"`
    //     AppId string `json:"app_id"`
    //     RoomId string `json:"room_id"`
    //     TaskId string `json:"task_id"`
    body: JSON.stringify({
      practice_id: practiceId?Number(practiceId):null,
      duration: duration,
      chat_message:chatMessage||[]
    }),
  })
  const data = await response.json()
  // console.log(data,'finishPractice')
   if(data.code===2000){
      return data 
  }else{
      Toast.info(data.msg)
  }
}

export const TrainingToken = async () => {
  const { timestamp, authorization } = generateAuthorization();
  let training_id = localStorage?.getItem('training_id')
  const response = await fetch(`${AIGC_PROXY_HOST}/ai-training/token`, {
    method: 'POST',
    headers: {
      'Content-type': 'application/json',
      'Authorization': authorization,
      'Timestamp': String(timestamp),
    },
    body: JSON.stringify({
      training_id: Number(training_id),
    }),
  })
  const data = await response.json()
   if(data.code===2000){
      return data 
  }else{
      Toast.info(data.msg)
  }
}